/**
 * \file QList.h
 * \brief Circular list/queue
 * \author Corey Tabaka
 */

/*
   Copyright 2007 Corey Tabaka

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

#include <ktypes.h>

#ifndef __USER_QLIST_H__
#define __USER_QLIST_H__

class QList {
	private:
		
	QList *prev;
	QList *next;
	
	public:
	
	QList(void);
	
	void clear(void);
	
	void prepend(QList *node);
	void append(QList *node);
	
	void remove(void);
	
	QList *removeHead(void);
	QList *removeTail(void);
	
	QList *head(void);
	QList *tail(void);
	
	QList *getNext(QList *node);
	
	bool isEmpty(void);
};

/**
 * Typed QList utilities. Requires access to 'member' of 'type'.
 */
#define QList_removeHead(list, type, member) ({		\
	QList *__node = list.removeHead();				\
	type *__t;										\
	if (__node) {									\
		__t = container_of(__node, type, member);	\
	} else {										\
		__t = (type *) 0;							\
	}												\
	__t;											\
})

#define QList_removeTail(list, type, member) ({		\
	QList *__node = list.removeTail();				\
	type *__t;										\
	if (__node) {									\
		__t = container_of(__node, type, member);	\
	} else {										\
		__t = (type *) 0;							\
	}												\
	__t;											\
})

#define QList_head(list, type, member) ({			\
	QList *__node = list.head();					\
	type *__t;										\
	if (__node) {									\
		__t = container_of(__node, type, member);	\
	} else {										\
		__t = (type *) 0;							\
	}												\
	__t;											\
})

#define QList_tail(list, type, member) ({			\
	QList *__node = list.tail();					\
	type *__t;										\
	if (__node) {									\
		__t = container_of(__node, type, member);	\
	} else {										\
		__t = (type *) 0;							\
	}												\
	__t;											\
})

#define QList_getNext(list, type, member) ({		\
	QList *__node = list.getNext();					\
	type *__t;										\
	if (__node) {									\
		__t = container_of(__node, type, member);	\
	} else {										\
		__t = (type *) 0;							\
	}												\
	__t;											\
})

#endif
